package com.beiding.dao.impl;


import com.beiding.dao.SettingsDao;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*

    该类是SettingsDao的辅助类

        1.初始化时从数据库中查看是否设置了各个参数,如果没有则从配置文件中获取默认配置进行配置,如果配置文件中没有配置响应的项,则异常终止

        2.维护配置项的名称


 */
@Component
@PropertySource("classpath:settings-default.properties")
public class SettingsHelper {


    private static final Map<String, String> timeoutNames = new HashMap<>();

    static {

        timeoutNames.put("pay", "支付等待时长");
        timeoutNames.put("accept", "受理超时时长");
        timeoutNames.put("delete", "受理超时时长");
        timeoutNames.put("refund", "退款超时时长");
        timeoutNames.put("checkIn", "入住超时时长");
        timeoutNames.put("unlock", "解锁订单超时时长");
        timeoutNames.put("withholdDeposit", "扣留押金超时时长");
        timeoutNames.put("evaluate", "评价超时时长");
        timeoutNames.put("withholdDepositDispute", "拒扣押金仲裁等待时长");
        timeoutNames.put("refundDeposit", "自动退还押金超时时长");

    }

    private SettingsDao settings;

    public SettingsHelper(SettingsDao settings, Environment environment) {

        this.settings = settings;

        //检查并配置参数

        Double rate = settings.getPlatformCostRate();

        if (rate == null) {

            String platformCostRate = environment.getProperty("platformCostRate");

            assert platformCostRate != null;

            //设置平台费率
            setPlatformCostRate(Double.valueOf(platformCostRate));

        }


        timeoutNames.keySet().forEach(s -> {

            Long t = settings.getTimeout(s);
            if (t == null) {
                String property = environment.getProperty(s);

                assert property != null;
                settings.setTimeout(s, Long.valueOf(property));
            }
        });


    }

    public void setPlatformCostRate(Double rate) {

        Assert.isTrue(rate >= 0 && rate < 1, "平台费率介于[0,1)");

        //采用收尾法保留小数点后五位
        rate = new BigDecimal(rate).setScale(5, RoundingMode.DOWN).doubleValue();

        settings.setPlatformCostRate(rate);

    }


    public Double getPlatformCostRate() {
        return settings.getPlatformCostRate();
    }


    public void setTimeout(String item, Long time) {
        Assert.isTrue(time > 0, "等待时长必须为正");
        settings.setTimeout(item, time);

    }



    public List<TimeoutItem> getTimeoutItems() {

        List<TimeoutItem> items = new ArrayList<>();

        timeoutNames.forEach((item, name) -> {

            Long timeout = settings.getTimeout(item);

            TimeoutItem timeoutItem = new TimeoutItem();
            timeoutItem.item = item;
            timeoutItem.value = timeout;
            timeoutItem.name = name;
            items.add(timeoutItem);


        });

        return items;
    }


    public static class TimeoutItem {

        public String item;

        public String name;

        public Long value;

    }



}
